#!/bin/sh

SOURCE=/mnt/koji/mash/updates
DEST=/pub/epel

OPTIONS="-rlptDvHh --stats --delay-updates $RSYNC_OPTS"

for rel in 6; do

    OUTPUT1=$(rsync $OPTIONS --exclude "repodata/*" --exclude "headers/*" \
            $SOURCE/dist-${rel}E-epel/ $DEST/${rel}/)
    OUTPUT2=$(rsync $OPTIONS --delete --delete-delay \
            $SOURCE/dist-${rel}E-epel/ $DEST/${rel}/)

    # Grep out some signals from the stats
    bytes=$(echo "$OUTPUT1" | grep "Literal data" | awk ' { print $3 } ')
    deleted=$(echo "$OUTPUT2" | grep "deleting " | wc -l)

    # If anything changed, then publish a fedmsg message as bodhi.updates.sync
    if [ "$bytes" != "0" -o "$deleted" != "0" ]; then
      echo "{\"bytes\": \"$bytes\", \"deleted\": \"$deleted\", \"repo\": \"epel\", \"release\": \"$rel\"}" | fedmsg-logger \
          --cert-prefix ftpsync \
          --modname bodhi \
          --topic updates.epel.sync \
          --json-input &> /dev/null
    fi

    OUTPUT1=$(rsync $OPTIONS --exclude "repodata/*" --exclude "headers/*" \
            $SOURCE/dist-${rel}E-epel-testing/ $DEST/testing/${rel}/)
    OUTPUT2=$(rsync $OPTIONS --delete --delete-delay \
            $SOURCE/dist-${rel}E-epel-testing/ $DEST/testing/${rel}/)

    # Grep out some signals from the stats
    bytes=$(echo "$OUTPUT1" | grep "Literal data" | awk ' { print $3 } ')
    deleted=$(echo "$OUTPUT2" | grep "deleting " | wc -l)

    # If anything changed, then publish a fedmsg message as bodhi.updates.sync
    if [ "$bytes" != "0" -o "$deleted" != "0" ]; then
      echo "{\"bytes\": \"$bytes\", \"deleted\": \"$deleted\", \"repo\": \"epel-testing\", \"release\": \"$rel\"}" | fedmsg-logger \
          --cert-prefix ftpsync \
          --modname bodhi \
          --topic updates.epel.sync \
          --json-input &> /dev/null
    fi
done

for rel in 7; do

    OUTPUT1=$(rsync $OPTIONS --exclude "repodata/*" \
            $SOURCE/epel${rel}/ $DEST/$rel/)
    OUTPUT2=$(rsync $OPTIONS --delete --delete-delay \
            $SOURCE/epel${rel}/ $DEST/$rel/)

    # Grep out some signals from the stats
    bytes=$(echo "$OUTPUT1" | grep "Literal data" | awk ' { print $3 } ')
    deleted=$(echo "$OUTPUT2" | grep "deleting " | wc -l)

    # If anything changed, then publish a fedmsg message as bodhi.updates.sync
    if [ "$bytes" != "0" -o "$deleted" != "0" ]; then
      echo "{\"bytes\": \"$bytes\", \"deleted\": \"$deleted\", \"repo\": \"epel\", \"release\": \"$rel\"}" | fedmsg-logger \
          --cert-prefix ftpsync \
          --modname bodhi \
          --topic updates.epel.sync \
          --json-input &> /dev/null
    fi

    OUTPUT1=$(rsync $OPTIONS --exclude "repodata/*" \
            $SOURCE/epel${rel}-testing/ $DEST/testing/${rel}/)
    OUTPUT2=$(rsync $OPTIONS --delete --delete-delay \
            $SOURCE/epel${rel}-testing/ $DEST/testing/${rel}/)

    # Grep out some signals from the stats
    bytes=$(echo "$OUTPUT1" | grep "Literal data" | awk ' { print $3 } ')
    deleted=$(echo "$OUTPUT2" | grep "deleting " | wc -l)

    # If anything changed, then publish a fedmsg message as bodhi.updates.sync
    if [ "$bytes" != "0" -o "$deleted" != "0" ]; then
      echo "{\"bytes\": \"$bytes\", \"deleted\": \"$deleted\", \"repo\": \"epel-testing\", \"release\": \"$rel\"}" | fedmsg-logger \
          --cert-prefix ftpsync \
          --modname bodhi \
          --topic updates.epel.sync \
          --json-input &> /dev/null
    fi
done

for rel in 6 7; do
    if [ ${rel} -eq 7 ]; then
	TARGET_DIR=${DEST}/${rel}/x86_64/e
    else
	TARGET_DIR=${DEST}/${rel}/x86_64
    fi

    if [ -f ${TARGET_DIR}/epel-release*rpm ]; then
	# We have a file to match. [This may sort wrong at -9 -> -10]
	CANDIDATE=$( ls ${TARGET_DIR}/epel-release-*rpm | sort | tail -n 1)
	TARGET=${DEST}/epel-release-latest-${rel}.noarch.rpm
	# Does our symbolic link exist?
	if [ -L ${TARGET} ]; then
	    # check to see if the link matches the candidate
	    TEST=$( readlink -e ${TARGET} )
	    if [ "${TEST}" != "${CANDIDATE}" ]; then
		ln -sf $(echo ${CANDIDATE}|sed -e "s|$DEST|./|g" -e 's|//|/|g') ${TARGET}
	    fi
	else
	    # first time for everything.
            ln -sf $(echo ${CANDIDATE}|sed -e "s|$DEST|./|g" -e 's|//|/|g') ${TARGET}
	fi
    else
	echo "No target file for epel-release ${rel} to link against."
    fi
done
